import sys

from pygame_test.SectorGfx import SectorGfx
import pygame


# 控制方向和角度
# 上下左右控制方向, 同时改变开口朝向
class EatBean:
    def __init__(self):
        winx = 300
        winy = 300
        pygame.init()
        screen = pygame.display.set_mode((winx, winy))
        x = 80
        y = 80
        rect_x = x
        rect_y = winy
        r = 50
        b =mouse1= 315 # 上嘴张开角度
        e =mouse2= 225 # 下嘴张开角度
        mouse0 = 270 # 嘴闭合的角度
        open_speed = 3
        speedx = 0
        speedy = 0
        mouse_open = False

        rects = []
        j = 0
        fclock = pygame.time.Clock()
        while True:
            x+=speedx
            y+=speedy

            # spin
            # b += 1
            # e +=1
            # mouse0+=1
            # mouse1+=1
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP:
                        speedx=0
                        speedy=-1
                        b = mouse1 = 135 # 上嘴张开角度
                        e = mouse2 = 45 # 下嘴张开角度
                        mouse0 = 90  # 嘴闭合的角度
                    elif event.key == pygame.K_DOWN:
                        speedx=0
                        speedy=1
                        b = mouse1 = 315  # 上嘴张开角度
                        e = mouse2 = 225  # 下嘴张开角度
                        mouse0 = 270  # 嘴闭合的角度
                    elif event.key == pygame.K_LEFT:
                        speedx=-1
                        speedy=0
                        b = mouse1 = 225 # 上嘴张开角度
                        e = mouse2 = 135  # 下嘴张开角度
                        mouse0 = 180  # 嘴闭合的角度
                    elif event.key == pygame.K_RIGHT:
                        speedx=1
                        speedy=0
                        b = mouse1 = 45  # 上嘴张开角度
                        e = mouse2 = 315  # 下嘴张开角度
                        mouse0 = 0  # 嘴闭合的角度

            sector = SectorGfx(x, y, r, (255, 255, 255), b, e)
            if b >= mouse1:
                mouse_open = False
            elif b <= mouse0:
                mouse_open = True
            if mouse_open:
                b += open_speed
                e -= open_speed
            else:
                b -= open_speed
                e += open_speed

            screen.fill((0, 0, 0))
            sector.draw(screen)
            # print(sector.x,sector.y)

            pygame.display.update()
            fclock.tick(40)

if __name__ == '__main__':
    eat_bean = EatBean()
